package com.jiesone.logger.distributecleaning.handler;
/*
 *@ClassName LoggerDistrbuteRedisReceiver
 *@Description TODO  智能加工业务
 *@Author tangsixiang@163.com
 *@Date 2022/1/18 15:12
 *@Version 1.0
 */

import com.alibaba.fastjson.JSON;
import com.jiesone.logger.distributecleaning.config.CommonRediskey;
import com.jiesone.logger.distributecleaning.util.ElasticSearchUtil;
import com.jiesone.logger.distributecleaning.util.JsonStringUtil;
import org.apache.lucene.queries.SearchAfterSortedDocQuery;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.ElasticsearchClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.reindex.UpdateByQueryRequest;
import org.elasticsearch.index.search.MatchQuery;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.query.QuerySearchRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class ResolveMachingRedisHandler {



    Logger logger= LoggerFactory.getLogger(ResolveMachingRedisHandler.class);
    private CountDownLatch latch;

    @Autowired
  ElasticSearchUtil  searchUtil;


    @Autowired
    public ResolveMachingRedisHandler(CountDownLatch latch) {
        this.latch = latch;
    }

    @Autowired
      StringRedisTemplate  redisTemplate;
  
    /**
     * 需要加工数据清洗流程业务处理
     *
     * //  key toINdex  value :fromIndex,fromFiled;tofiled;filed  
     * @param message
     */
    public void receiverMessage(String  message) {
        try {
        logger.debug("---------------进入清洗数--道加工通道 maching-----------------"+message);
        Map<String ,Object>  map= JSON.parseObject(message,HashMap.class);
                String indeName=map.get("indexName").toString().toLowerCase();
      if( redisTemplate.opsForHash().hasKey(CommonRediskey.machingRule,indeName)) {
          String[] toIndexStr = redisTemplate.opsForHash().get(CommonRediskey.machingRule,indeName).toString().split(";");
          Map queryMap = new HashMap();
          queryMap.put(toIndexStr[1], map.get(toIndexStr[2]));

          SearchHits resp = searchUtil.searchQuery(queryMap, toIndexStr[0]);
          Iterator<SearchHit> it = resp.iterator();
          Map<String, Object> mapes = it.next().getSourceAsMap();
          String[] tmp = toIndexStr[3].split(",");
          Long id = (Long) map.get("id");
          map.remove("id"); map.remove("indexName");
          for (String str: tmp) {
              if (mapes.containsKey(str)) {
                  map.put(str, mapes.get(str));
              }

          }
                  searchUtil.insertOrUpdateOne(indeName, id, map);

      }
          }  catch (Exception exception) {
            exception.printStackTrace();
        }
    }




}
